package com.gy.assistant.ui.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import im.zego.zim.ZIM;
import com.gy.assistant.KeyCenter;
import com.gy.assistant.MyApplication;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.BaseActivity;
import com.gy.assistant.util.PreferenceUtil;
import com.gy.assistant.util.widget.CustomEditText;

public class SettingActivity extends BaseActivity {
    CheckBox cbAppSign;
    CustomEditText edAppID;
    CustomEditText edToken;
    CustomEditText edAppType;
    CustomEditText edAppSign;
    CustomEditText edAppResourceId;

    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, SettingActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        cbAppSign = findViewById(R.id.cb_app_sign);
        edAppID = findViewById(R.id.ed_app_id);
        edToken = findViewById(R.id.ed_app_token);
        edAppSign = findViewById(R.id.ed_app_sign);
        edAppType =findViewById(R.id.ed_app_type);
        edAppResourceId = findViewById(R.id.ed_resource_id);
        TextView tv_version = findViewById(R.id.tv_version);

        cbAppSign.setChecked(MyApplication.enableAppSign);
        edAppID.setText(String.valueOf(KeyCenter.appID));
        edToken.setText(KeyCenter.token);
        edAppSign.setText(KeyCenter.appSign);

        tv_version.setText("SDK Version:"+ ZIM.getVersion());
    }

    public void save(View view) {
        String token = edToken.getText() == null ? "" : edToken.getText().toString();
        String appType = edAppType.getText() == null ? "" : edAppType.getText().toString();
        String sign = edAppSign.getText() == null ? "" : edAppSign.getText().toString();

        if (edAppID.getText() != null && (!TextUtils.isEmpty(token) || !TextUtils.isEmpty(sign))) {
            KeyCenter.appID = Long.parseLong(edAppID.getText().toString());
            KeyCenter.token = token;
            KeyCenter.appSign = sign;
            String appSign = cbAppSign.isChecked() ? edAppSign.getText().toString() : "";
            PreferenceUtil.getInstance(this).setStringValue("appType", appType);
            PreferenceUtil.getInstance(this).setBooleanValue("enableAppSign", cbAppSign.isChecked());
            MyApplication.enableAppSign = cbAppSign.isChecked();

            SDKManager.sharedInstance().destroy();
            SDKManager.sharedInstance().create(this.getApplication(), appSign);
            Toast.makeText(this, getString(R.string.save_success), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, getString(R.string.save_failed), Toast.LENGTH_SHORT).show();
        }
        finish();
    }
}
